package com.example.api.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.mybatis.gen.dao.SysUserMapper;
import com.common.mybatis.gen.model.SysFunc;
import com.common.mybatis.gen.model.SysRole;
import com.common.mybatis.gen.model.SysUser;
import com.common.mybatis.gen.model.SysUserExample;
import com.example.api.dao.CustomSysUserMapper;
import com.example.api.service.CustomSysUserService;

@Service
public class CustomSysUserServiceImpl implements CustomSysUserService {

	@Autowired
	private CustomSysUserMapper customSysUserMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Override
	public List<SysRole> getUserSysRoles(Integer userId) {
		if(userId == null) return null;
		return customSysUserMapper.getUserSysRoles(userId);
	}

	@Override
	public List<SysFunc> getUserSysFuncs(Integer userId) {
		if(userId == null) return null;
		return customSysUserMapper.getUserFuncs(userId);
	}

	@Override
	public SysUser getUserByName(String userName) {
		//判空处理
		if(StringUtils.isBlank(userName)) return null;
		
		//设置查询条件
		SysUserExample example = new SysUserExample();
		example.createCriteria().andUsernameEqualTo(userName);
		
		List<SysUser> list = sysUserMapper.selectByExample(example );
		
		//返回查询到的第一个元素，实际上list最多只有一个元素
		if(list != null && list.size() > 0) return list.get(0);
		
		//未查询到，返回null
		return null;
	}

}
